package com.atguigu.system.service.impl;

import com.atguigu.common.helper.MenuHelper;
import com.atguigu.common.helper.RouterHelper;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.model.vo.SysUserQueryVo;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysUserMapper;
import com.atguigu.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public IPage<SysUser> findPage(Page<SysUser> page, SysUserQueryVo userQueryVo) {
        return sysUserMapper.findPage(page,userQueryVo);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //创建SysUser对象
        SysUser sysUser = new SysUser();
        //设置id和状态
        sysUser.setId(id);
        sysUser.setStatus(status);
        //调用SysUserMapper中更新的方法
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public SysUser getSysUserByUsername(String username) {
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username));
    }

    @Override
    public Map<String, Object> getUseInfoByUserId(Long userId) {
        //根据用户id获取SysUser对象
        SysUser sysUser = sysUserMapper.selectById(userId);

        //调用当类中的根据用户id获取用户权限菜单的方法
        List<SysMenu> userMenuList = getUserMenuByUserId(userId);

        //通过MenuHelper工具类将权限菜单转换为菜单树
        List<SysMenu> userMenuTree = MenuHelper.buildTree(userMenuList);

        //通过RouterHelper工具类将权限菜单树转换为路由
        List<RouterVo> userRouter = RouterHelper.buildRouters(userMenuTree);

        //调用当前类中根据用户id获取用户按钮权限标识符的方法
        List<String> userBtnPermsList = getUserBtnPermsByUserId(userId);

        //创建一个Map
        Map<String,Object> map = new HashMap();
        //设置用户姓名
        map.put("name",sysUser.getUsername());
        //设置用户的头像，正常也是从SysUser中获取用户的头像
//        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("avatar",sysUser.getHeadUrl());
        //将用户路由放到map中
        map.put("routers",userRouter);
        //将用户的按钮权限标识符放到map中
        map.put("buttons",userBtnPermsList);
        return map;
    }

    @Override
    public List<String> getUserBtnPermsByUserId(Long userId) {
        //调用当类中的根据用户id获取用户权限菜单的方法
        List<SysMenu> userMenuList = getUserMenuByUserId(userId);
        //创建一个保存用户按钮权限标识符的集合
        List<String> userBtnPermsList = new ArrayList<>();
        //遍历用户的权限菜单
        for (SysMenu sysMenu : userMenuList) {
            if(sysMenu.getType() == 2){
                //证明这是一个按钮，将按钮的权限标识符放到userBtnPermsList中
                userBtnPermsList.add(sysMenu.getPerms());
            }
        }
        return userBtnPermsList;
    }

    //在当前实现类中创建一个根据用户id获取用户权限菜单的方法
    public List<SysMenu> getUserMenuByUserId(Long userId){
        //创建一个保存用户权限菜单的集合
        List<SysMenu> userMenuList = null;
        if(userId == 1L){
            //证明是系统管理员，获取所有的权限菜单
            userMenuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().eq("status",1).orderByAsc("sort_value"));
        }else {
            //根据用户id获取用户的权限菜单
            userMenuList = sysMenuMapper.getUserMenuListByUserId(userId);
        }
        return userMenuList;
    }
}
